local lianju = fk.CreateSkill{
  name = "doublez__lianju"
}

Fk:loadTranslationTable{
  ["doublez__lianju"] = "连裾",
  [":doublez__lianju"] = "出牌阶段，你可以令任意名手牌数和为4的角色摸一张牌；这些角色本回合非锁定技失效直到你失去基本牌。",

  ["@@doublez__lianju-turn"] = "连裾",
  ["#doublez__lianju"] = "连裾：你可以令任意名手牌数和为4的角色摸一张牌"
}

lianju:addEffect("active",{
  target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
    local card_num = 0
    for _, p in ipairs(selected) do
      card_num = card_num + p:getHandcardNum()
    end
    return 4 - card_num - to_select:getHandcardNum() >= 0
  end,
  card_filter = Util.FalseFunc,
  can_use = function (self, player)
    for _, p in ipairs(Fk:currentRoom().alive_players) do
      if p:getHandcardNum() <= 4 then
        return true
      end
    end
  end,
  prompt = "#doublez__lianju",
  feasible = function (self, player, selected, selected_cards, card)
    local card_num = 0
    for _, p in ipairs(selected) do
      card_num = card_num + p:getHandcardNum()
    end
    return card_num == 4
  end,
  on_use = function (self, room, effect)
    local targets = table.simpleClone(effect.tos)
    room:sortByAction(targets)
    local player = effect.from
    for _, target in ipairs(targets) do
      target:drawCards(1 ,lianju.name)
      room:setPlayerMark(target, "@@doublez__lianju-turn", 1)
      room:setPlayerMark(target, MarkEnum.UncompulsoryInvalidity.."-turn", player.id)
    end
  end
})

lianju:addEffect(fk.AfterCardsMove, {
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(self, true, true) then return end
    if not table.find(player.room:getAlivePlayers(), function (p)
      return p:getMark("@@doublez__lianju-turn") == 1 and p:getMark(MarkEnum.UncompulsoryInvalidity.."-turn") == player.id
    end) then return end
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId).type == Card.TypeBasic then
            return true
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local tos = table.filter(player.room:getAlivePlayers(), function (p)
      return p:getMark("@@doublez__lianju-turn") == 1 and p:getMark(MarkEnum.UncompulsoryInvalidity.."-turn") == player.id
    end)
    for _, p in ipairs(tos) do
      player.room:setPlayerMark(p, "@@doublez__lianju-turn", 0)
      player.room:setPlayerMark(p, MarkEnum.UncompulsoryInvalidity.."-turn", 0)
    end
  end
})

return lianju